package com.qf.service.impl;

import com.qf.entity.Shop;
import com.qf.entity.ShopExample;
import com.qf.entity.UserShare;
import com.qf.entity.custom.ShopCustom;
import com.qf.mapper.ShopMapper;
import com.qf.mapper.custom.ShopCustomMapper;
import com.qf.service.ShopCustomService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @Description TODO
 * @Author 刘珈玮
 * @Date 2020/7/23 21:49
 */
@Service
public class ShopCustomServiceImpl implements ShopCustomService {
    @Autowired
    private ShopCustomMapper shopCustomMapper;
    @Autowired
    private ShopMapper shopMapper;

    /**
     * 查询所有商品信息和店铺信息
     * @return 返回一个shopCustom实体类型的list集合
     */
    @Override
    public List<ShopCustom> getShop_Product(String productType) {
        return shopCustomMapper.selectShop_Product(productType);
    }

    /**
     * 根据店铺距离排序 按升序排序 查询所有店铺商品信息
     * @return 返回一个ShopCustom实体类型的list集合
     */
    @Override
    public List<ShopCustom> getShop_ProductByDistance() {
        return shopCustomMapper.selectShop_ProductByDistance();
    }

    /**
     * 根据店铺评分排序 按降序排序 查询所有店铺商品信息
     * @return 返回一个ShopCustom实体类型的list集合
     */
    @Override
    public List<ShopCustom> getShop_ProductByPoint() {
        return shopCustomMapper.selectShop_ProductByPoint();
    }

    /**
     * 根据店铺销量排序 按降序排序 查询所有店铺商品信息
     * @return 返回一个ShopCustom实体类型的list集合
     */
    @Override
    public List<ShopCustom> getShop_ProductBySalesnum() {
        return shopCustomMapper.selectShop_ProductBySalesnum();
    }

    @Override
    public List<ShopCustom> getShop_ProductByExample(String isfree, String isholiday, Integer isnew) {
        return shopCustomMapper.selectShop_ProductByExample(isfree, isholiday, isnew);
    }

    /**
     * 根据搜索次数降序查询shop表前三条数据
     * @return 返回List<ShopCustom>的数据
     */
    @Override
    public List<ShopCustom> getShopOrderByCount() {
        return shopCustomMapper.selectShopByCount();
    }

    /**
     * 用户搜索后增加count数量
     * @param id 点击店铺id
     * @return 增加成功返回true 失败返回false
     */
    @Override
    public boolean updateShopCount(int id) {
        ShopExample example = new ShopExample();
        ShopExample.Criteria criteria = example.createCriteria();
        Shop shop = shopMapper.selectByPrimaryKey(id);
        if (shop != null){
            Integer shopCount = shop.getShopCount();
            int num = ++shopCount;
            shop.setShopCount(num);
            int i = shopMapper.updateByPrimaryKeySelective(shop);
            if (i != 0) {
                return true;
            }else {
                return false;
            }
        }else{
            return false;
        }
    }

    /**
     * 用户搜索，店铺名关键词
     * @param search 店铺名关键词
     * @return 搜索成功返回true和Shop集合数据 失败返回false
     */
    @Override
    public List<Shop> selecShopByNameKey(String search) {

        ShopExample example = new ShopExample();
        ShopExample.Criteria criteria = example.createCriteria();
        if (search != null && search!=""){
            criteria.andShopNameLike("%"+search+"%");
            List<Shop> shops = shopMapper.selectByExampleWithBLOBs(example);
            if (shops != null){
                for (Shop s :shops) {
                    boolean b = modifyShopSearch(s);
                    if (b =false){
                        return null;
                    }
                }
                return shops;
            }else {
                return null;
            }
        }else {
            return null;
        }
    }

    /**
     * 用户搜索，店铺名关键词
     * @param search 店铺名关键词
     * @return 搜索成功返回true和Shop集合数据 失败返回false
     */
    @Override
    public List<Shop> selectShopByNameKey(String search) {

        ShopExample example = new ShopExample();
        ShopExample.Criteria criteria = example.createCriteria();
        if (search != null && search!=""){
            criteria.andShopNameLike("%"+search+"%");
            List<Shop> shops = shopMapper.selectByExampleWithBLOBs(example);
            if (shops != null){
                for (Shop s :shops) {
                    Integer shopId = s.getShopId();
                    Shop shop = shopMapper.selectByPrimaryKey(shopId);
                    Integer shopCount = shop.getShopCount();
                    int num = shopCount++;
                    shop.setShopCount(num);
                }
                return shops;
            }else {
                return null;
            }
        }else {
            return null;
        }
    }

    /**
     * 根据店铺id查询店铺详细信息 以及 该店铺所含套餐信息
     * @param shopId 店铺id
     * @return 返回一个ShopCustom实体类型的list集合
     */
    public List<ShopCustom> getShopSetMeal(Integer shopId) {
        if (shopId != null) {
            List<ShopCustom> shopCustoms = shopCustomMapper.selectShopSetMeal(shopId);
            if (shopCustoms != null) {
                return shopCustoms;
            } else {
                return null;
            }
        } else {
            return null;
        }
    }

    /**
     * 当查询时就增加点击shopCount
     * @param shop 传入shop实体类
     * @return 增加成功返回true 失败返回false
     */
    @Override
    public boolean modifyShopSearch(Shop shop) {
        Integer shopCount = shop.getShopCount();
        if (shopCount != null) {
            int num = ++shopCount;
            shop.setShopCount(num);
            int i = shopMapper.updateByPrimaryKeySelective(shop);
            if (i != 0) {
                return true;
            } else {
                return false;
            }
        } else {
            return false;
        }
    }

}
